package acs.bridge.jreport;

import org.apache.log4j.Logger;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * acs.jreport.JReportTemplateFile
 * User: KuznetsovStV
 * Date: 24.12.11
 * Time: 22:36
 * for loading report template from file
 */
public class JReportTemplateFile implements JReportTemplateInterface {
    Logger logger = Logger.getLogger(this.getClass().getName());

    public void setTemplateFilePath(String templateFilePath) {
        this.templateFilePath = templateFilePath;
    }

    private String templateFilePath;

    private String templateFileName;

    public void setTemplateFileName(String templateFileName) {
        this.templateFileName = templateFileName;
    }

    public static String getFileNameWithoutExtension(String fileName) {

        int whereDot = fileName.lastIndexOf('.');
        if (0 < whereDot && whereDot <= fileName.length() - 2) {
            return fileName.substring(0, whereDot);
            //extension = filename.substring(whereDot+1);
        }
        return "";
    }

    public String getReportName() {
        return getFileNameWithoutExtension(templateFileName);
    }

    public InputStream getBinaryStream() throws FileNotFoundException {
        logger.info("Loading template {filePath=" + templateFilePath + ",fileName=" + templateFileName + "}");
        return new FileInputStream(templateFilePath + templateFileName);
    }
}
